const md = require('../../utils/md5.js');
const app = getApp();
const post = require('../../utils/post.js');
var util = require('../../utils/util.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    charge: 0,
    money: 0,
    tixianing: 0,
    index: 0,
    region: ['福建省', '厦门市', '思明区'],
    bankarray: [{
      name: '请选择支行',
      id: '1'
    }],
    bankindex: 0,
    showbanks: "请选择支行",
    showarea:'请选择地区'
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this;
    that.getjieyu();
    that.showhistory();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    var showbanks = wx.getStorageSync('cbankname');
    var bankscode = wx.getStorageSync('cbankno');
    if (showbanks) {
      this.setData({
        showbanks: showbanks
      })
    }else{
      let bankdetail = wx.getStorageSync('userinfo').BankDetail;
      if (bankdetail){
        this.setData({
          showbanks: bankdetail
        })
      }
    }
    if (bankscode) {
      this.setData({
        bankscode: bankscode
      })
    }
  },
  onUnload: function() {},
  bindRegionChange: function(e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      region: e.detail.value,
      showarea: e.detail.value[0] + "/" + e.detail.value[1] + "/" + e.detail.value[2],
      province: e.detail.value[0],
      city: e.detail.value[1],
      area: e.detail.value[2],
      areatag: 1
    })
    // this.getbanks(e.detail.value[1])
  },
  bindBankChange: function(e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      bankindex: e.detail.value,
      showbanks: this.data.bankarray[e.detail.value].name,
      bankscode: this.data.bankarray[e.detail.value].id
    })
  },
  getbanks: function(cityname = '', isfirst = 0) {
    var that = this;
    if (!cityname) {
      cityname = this.data.city;
    }
    if (isfirst == 1) {
      if (that.data.userinfo.CityName) {
        cityname = that.data.userinfo.CityName;
      } else {
        wx.showModal({
          title: '提示',
          content: '请先选择地区，否则无法选择支行信息！',
        })
        return;
      }
    }
    if (this.data.show == '请选择') {
      wx.showModal({
        title: '提示',
        content: '请先选择开户银行！',
      })
      return;
    }
    var datas = {};
    datas.where = "CityName =" + cityname + " and BankName like '%" + this.data.show + "%'";
    var datacall = function(res) {
      console.log(res);
      if (res.data.data.length > 0) {
        var banklist = [];
        var ll = {};
        for (var i = 0; i < res.data.data.length; i++) {
          ll.name = res.data.data[i].CBankName;
          ll.id = res.data.data[i].CBankNo;
          banklist.push(ll);
          ll = {};
        }
        that.setData({
          bankarray: banklist
        })
      }
    }
    var res2 = post.getdata("/BO/BA_BankDetail/Query", datas, datacall);
  },
  getcharge: function(accid = 0) {
    var that = this;
    // 获取时间戳
    var datas = {};
    datas.AccID = accid;
    var datacall = function(res) {
      console.log(res);
      if (res.data.data.length != 0) {
        let maxbalance =0;
        if (res.data.data.Balance>0){
          maxbalance = res.data.data.Balance * 0.001;
          maxbalance = Math.ceil(maxbalance*100)/100;
          maxbalance = res.data.data.Balance - maxbalance;
          maxbalance = post.toDecimal2(maxbalance);
        }
        that.setData({
          feerate: res.data.data.FeeRate,
          feelow: res.data.data.FeeLow,
          detail: res.data.data.Detail,
          balance: res.data.data.Balance,
          maxbalance:maxbalance
        })
      }
    }
    var res2 = post.getdata("/Action/ZKPay/CashApplyRule", datas, datacall);
  },
  getjieyu: function() {
    var that = this;
    // 获取时间戳
    var datas = {};
    var phone = (wx.getStorageSync('phone'));
    datas.where = 'Tel =' + phone;
    var datacall = function(res) {
      console.log(res);
      if (res.data.data.length != 0) {
        if (res.data.data[0].Balance) {
          res.data.data[0].Balance = post.toDecimal2(res.data.data[0].Balance)
        }
        var userinfo = res.data.data[0];
        wx.setStorageSync('userinfo', res.data.data[0]);
        that.setData({
          userinfo: userinfo,
          show: res.data.data[0].BankName,
        });
        if (res.data.data[0]._BOState != 100) {
          wx.showModal({
            title: '',
            content: '您还未进行实名认证或者实名认证未通过，请先进行实名认证~',
            success: function(res) {
              if (res.confirm) {
                wx.switchTab({
                  url: '/pages/mine/mine',
                })
              } else if (res.cancel) {
                wx.navigateBack({
                  delta: 1
                })
              }
            }
          })
          return;
        }
        if (!userinfo.CityName) {
          // wx.showModal({
          //   title: '提示',
          //   content: '请先选择地区，再选择支行信息！',
          // })
        } else {
          that.setData({
            showarea: userinfo.ProName + "/" + userinfo.CityName + "/" + userinfo.AreaName
          })
          // if (!userinfo.CBankNo) {
          //   that.getbanks(userinfo.CityName);
          // }
        }
        that.getcharge(wx.getStorageSync('depid'));

      }
    }
    var res2 = post.getdata("/BO/BA_Member/Query", datas, datacall);
  },
  formSubmit: function(e) {
    var tixianing = this.data.tixianing;
    console.log('form发生了submit事件，携带数据为：', e.detail.value);
    var that = this;
    var money = e.detail.value.money;
    if (!money || money <= 0) {
      wx.showModal({
        title: '',
        content: '请输入正确的提现金额',
      })
      return;
    }
    money = parseFloat(money);
    var lastmoney = that.data.userinfo.Balance;
    lastmoney = parseFloat(lastmoney);
    if (lastmoney < money) {
      wx.showModal({
        title: '',
        content: '余额不足！',
      })
      return;
    }
    var charge = that.data.charge ? that.data.charge + 0.00 : 0.00;
    if ((charge + money) > lastmoney) {
      wx.showModal({
        title: '',
        content: '超出了可用余额！',
      })
      return;
    }
    // console.log(that.data.balance)
    // console.log(money)
    if (that.data.balance == money && that.data.feerate!=0){
      wx.showModal({
        title: '',
        content: '剩余的资金结余不足以支付提现手续费￥' + that.data.charge + ",当前最大可提现金额为￥" + that.data.maxbalance +" \r\n 是否全部提现？",
        success:function(res){
          if (res.confirm) {
            wx.showLoading({
              title: '正在提现',
            })
            if (tixianing != 0) {
              return;
            }
            that.setData({
              tixianing: 1
            })
            var data = {};
            var datas = {};
            datas.MebID = that.data.userinfo.MebID;
            datas.AccID = wx.getStorageSync('depid');
            datas.OutMoney = that.data.maxbalance;
            datas.Tel = that.data.userinfo.Tel;
            if (that.data.showbanks) {
              datas.BankDetail = that.data.showbanks;
            } else {
              datas.BankDetail = that.data.userinfo.BankDetail;
            }
            if (that.data.bankscode) {
              datas.CBankNo = that.data.bankscode;
            } else {
              datas.CBankNo = that.data.userinfo.CBankNo;
            }
            datas.BankName = that.data.userinfo.BankName;
            datas.BankCardNo = that.data.userinfo.BankCardNo;
            datas.MebName = that.data.userinfo.MebName;
            datas.OverMoney = 0;
            datas.ACADate = util.formatTime(new Date());
            datas.Charge = that.data.charge ? that.data.charge : 0.00;
            console.log(datas);
            datas = JSON.stringify(datas);
            data.data = datas;

            var url = "/BO/AP_CashPayApply/Add";
            var callback = function (res) {
              wx.hideLoading();
              console.log(res);
              if (res.data.data != '') {
                that.getjieyu();
                that.showhistory();
                that.setData({
                  moneys: 0,
                  tixianing: 0
                })
              }
            };
            var spec = function (res) {
              that.setData({
                tixianing: 0
              })
              wx.hideLoading();
              wx.showModal({
                title: '',
                content: res.data.msg,
              })
              return;
            };
            var res = post.getdata(url, data, callback, spec);
          } else {
            return;
          }
        }
      })
      return;
    }else{
      wx.showLoading({
        title: '正在提现',
      })
      if (tixianing != 0) {
        return;
      }
      that.setData({
        tixianing: 1
      })
      var data = {};
      var datas = {};
      datas.MebID = that.data.userinfo.MebID;
      datas.AccID = wx.getStorageSync('depid');
      datas.OutMoney = money;
      datas.Tel = that.data.userinfo.Tel;
      if (that.data.showbanks) {
        datas.BankDetail = that.data.showbanks;
      } else {
        datas.BankDetail = that.data.userinfo.BankDetail;
      }
      if (that.data.bankscode) {
        datas.CBankNo = that.data.bankscode;
      } else {
        datas.CBankNo = that.data.userinfo.CBankNo;
      }
      datas.BankName = that.data.userinfo.BankName;
      datas.BankCardNo = that.data.userinfo.BankCardNo;
      datas.MebName = that.data.userinfo.MebName;
      datas.OverMoney = lastmoney - money - 0;
      datas.ACADate = util.formatTime(new Date());
      datas.Charge = that.data.charge ? that.data.charge : 0.00;
      console.log(datas);
      datas = JSON.stringify(datas);
      data.data = datas;

      var url = "/BO/AP_CashPayApply/Add";
      var callback = function (res) {
        wx.hideLoading();
        console.log(res);
        if (res.data.data != '') {
          that.getjieyu();
          that.showhistory();
          that.setData({
            moneys: 0,
            tixianing: 0
          })
        }
      };
      var spec = function (res) {
        that.setData({
          tixianing: 0
        })
        wx.hideLoading();
        wx.showModal({
          title: '',
          content: res.data.msg,
        })
        return;
      };
      var res = post.getdata(url, data, callback, spec);
    }
   
  },
  bindPickerChange: function(e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      index: e.detail.value,
      show: this.data.array[e.detail.value]
    })
  },
  tohistory: function() {
    wx.navigateTo({
      url: '/pages/thistory/thistory',
    })
  },
  showhistory: function() {
    var that = this;
    var data = {};
    data.where = "MebID =" + wx.getStorageSync('userinfo').MebID + " and _BOState = 0";
    var url = "/BO/AP_CashPayApply/Query";
    var callback = function(res) {
      console.log(res);
      if (res.data.data != '') {
        that.setData({
          applyinfo: res.data.data,
          showapply: 1
        })
      } else {
        that.setData({
          applyinfo: [],
          showapply: 0
        })
      }
    };
    var res = post.getdata(url, data, callback);
  },
  cancer: function(e) {
    var id = e.currentTarget.dataset.id;
    var that = this;

    wx.showModal({
      title: '提示',
      content: '是否确定撤销提现申请！！',
      success: function(res) {
        if (res.confirm) {
          var data = {};
          var datas = {};
          datas.ACACode = id;
          datas._BOState = -10;
          data.data = JSON.stringify(datas);
          var url = "/BO/AP_CashPayApply/Modify";
          var callback = function(res) {
            console.log(res);
            that.getjieyu();
            that.showhistory();
          };
          var res = post.getdata(url, data, callback);
        } else {

        }
      }
    })

  },
  getkey: function(e) {
    var that = this;
    if (that.data.feerate == 0) {
      that.setData({
        charge: 0
      })
      return;
    }
    var money = e.detail.value;
    var charge = that.data.feelow;
    let num1 = Math.floor((money * 0.01 * that.data.feerate)*1000)/1000;
    var num = Math.ceil(num1*100)/100;
    if (num > charge) {
      charge = num;
    }
    charge = post.toDecimal2(charge)
    that.setData({
      money: e.detail.value,
      charge: charge
    })
  },
  tobankson: function () {
    var banknmame = this.data.show;
    var city = this.data.city ? this.data.city : this.data.userinfo.CityName;
    if (!city || city=='请选择地区') {
      wx.showModal({
        title: '',
        content: '请先选择开户地区！',
      })
      return;
    }
    wx.navigateTo({
      url: '/pages/bankson/bankson?bankname=' + banknmame + "&city=" + city,
    })
  }
})